# -*- coding: utf-8 -*-
# FileName:     rec.py
# time:         23/4/26 026 下午 8:01
# Author:       Zhou Hang
# Description:  矩形阵列
from pathlib import Path
import cv2

from core.rec_matrix import rec_matrix

source_file = Path("./src.jpg")  # 源文件
if not source_file.exists():
    print("原始图像不存在，请将待处理文件重命名为src.jpg, 放在当前目录下")
    exit(0)

temp_path = Path("./resources/temp")  # 中间结果目录
result_path = Path("./resources/res")  # 结果存放目录
temp_path.mkdir(parents=True, exist_ok=True)
result_path.mkdir(parents=True, exist_ok=True)

img = cv2.imread(str(source_file))
print(f"源文件横坐标x范围: 0-{img.shape[1]} 纵坐标y范围: 0-{img.shape[0]}")

# 给定长宽和中心坐标
# width, height = 900, 900
# cx, cy = 1000, 1000

width = int(input("请输入截取矩形宽度: "))
height = int(input("请输入截取矩形高度: "))
cx = int(input("请输入截取矩形中心横坐标x: "))
cy = int(input("请输入截取矩形中心纵坐标y: "))

uniq_name = f"x{cx}_y{cy}_{height}_{width}"
result_file = result_path / f"{uniq_name}.jpg"

# 截取对应的矩形并保存
unit = img[cy - height // 2: cy + height // 2, cx - width // 2: cx + width // 2]
unit_path = temp_path / f"{uniq_name}_unit.jpg"
cv2.imwrite(str(unit_path), unit)

rec = unit
new_rec = rec_matrix(rec, 5, 3)
cv2.imwrite(str(result_file), new_rec)
print(f"生成完毕，图片保存至: {result_file}")
